package com.shenmazong.thread;

import java.util.concurrent.*;

/**
 * @program: server-java-demo
 * @description: ThreadMethod3
 * @author: 亮子说编程
 * @create: 2020-10-14 20:33
 **/

class ThreadCallable implements Callable<Integer> {
    public Integer call() throws Exception {
        for (int i = 0; i < 20; i++) {
            System.out.println(Thread.currentThread().getName()+",i="+i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return 0;
    }
}

public class ThreadMethod3 {
    /**
    * @Description: 第一种方法
    * @Param: [args]
    * @return: void
    * @Author: 亮子说编程
    * @Date: 2020/11/12 8:17
    */
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        FutureTask<Integer> task = new FutureTask<>(new ThreadCallable());
        new Thread(task, "aaa").start();

        System.out.println(task.get());
    }

//    public static void main(String[] args) {
//
//        //--1 创建对象
//        ThreadCallable threadCallable = new ThreadCallable();
//
//        //--2 创建线程池
//        ExecutorService executorService = Executors.newFixedThreadPool(1);
//
//        //--3 提交运行
//        Future<Integer> submit = executorService.submit(threadCallable);
//
//        //--4 获取结果
//        Integer result = null;
//        try {
//            result = submit.get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//
//        //--5 销毁线程池
//        executorService.shutdownNow();
//
//        System.out.println("result="+result);
//    }
}
